<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017/5/9
 * Time: 15:23
 */

use yii\widgets\LinkPager;
use yii\helpers\Url;

$web = Url::base();
$this->title = '操作日志';
?>

<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>

<!--search-->
<div class="panel panel-frame panel-operat">
    <div class="panel-body">
        <div class="form-inline">
            <div class="col-lg-1">
                <input type="text" class="form-control" id="controllerId" placeholder="controllerId" style="width: 120px"
                       value="<?= $controllerId ?>">
            </div>
            <div class="col-lg-1">
                <input type="text" class="form-control" id="actionId" placeholder="actionId" style="width: 120px"
                       value="<?= $actionId ?>">
            </div>
            <div class="col-lg-2">
                <input type="number" class="form-control" id="durationTime" placeholder="大于请求时间(毫秒)"
                       value="<?= $durationTime ?>">
            </div>
            <div class="col-lg-2">
                            <span class="input-group date form_date" data-date="" data-date-format="yyyy-mm-dd"
                                  data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                                <input class="form-control" size="16" type="text" readonly id="startTime"
                                       name="startTime"
                                       placeholder="开始时间" value="<?= $startTime ?>">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                <span class="input-group-addon"><span
                                            class="glyphicon glyphicon-calendar"></span></span>
                            </span>
                <input type="hidden" id="dtp_input2" value=""/>
            </div>

            <div class="col-lg-2">
                            <span class="input-group date form_date" data-date="" data-date-format="yyyy-mm-dd"
                                  data-link-field="dtp_input3" data-link-format="yyyy-mm-dd">
                                <input class="form-control" size="16" type="text" readonly id="endTime" name="endTime"
                                       placeholder="结束时间" value="<?= $endTime ?>">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                <span class="input-group-addon"><span
                                            class="glyphicon glyphicon-calendar"></span></span>
                            </span>
                <input type="hidden" id="dtp_input3" value=""/>
            </div>
            <div class="col-lg-2">
                <select class="select-group" name="apiType" id="apiType">
                    <option value="-1">全部</option>
                    <option value="0" <?= $apiType == 0 ? 'selected' : '' ?>>通用</option>
                    <option value="1" <?= $apiType == 1 ? 'selected' : '' ?>>用户端</option>
                    <option value="2" <?= $apiType == 2 ? 'selected' : '' ?>>商户端</option>
                </select>
            </div>
            <div class="col-lg-1">
                <button id="btnSearch" type="button" class="btn btn-primary" onclick="filter()">
                    <i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>

<!--列表-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>控制器</th>
                <th>action id</th>
                <th>api类型</th>
                <th>创建时间</th>
                <th>请求持续时间(毫秒)</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($records as $record) {
                ?>
                <tr>
                    <td><?= $record['controllerId'] ?></td>
                    <td><?= $record['actionId'] ?></td>
                    <td>
                        <?php
                        $apiTypeStr = null;
                        switch ($record['apiType']) {
                            case '0':
                                $apiTypeStr = '通用';
                                break;
                            case '1':
                                $apiTypeStr = '用户';
                                break;
                            case '2':
                                $apiTypeStr = '商户';
                                break;
                        }
                        ?>
                        <?= $apiTypeStr ?></td>
                    <td><?= date('Y-m-d H:i:s', $record['createDatetime']) ?></td>
                    <td><?= $record['durationTime'] ?></td>
                    <td>
                        <div class="btn-group">
                            <a class="btn btn-link" data-toggle="modal" data-log-id="<?= $record['_id'] ?>"
                               href="#detailModal">查看详情</a>
                        </div>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
    </div>
    <div class="table-pagination">
        <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
    </div>
</div>


<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">详情</h4>
            </div>
            <div id="detail_content">
                正在加载数据....
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    $('.form_date').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });

    $(function () {
        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('<div class="modal-body">正在加载数据....</div>');
            var logId = e.relatedTarget.attributes['data-log-id'].value;
            requestHtml('api-operate-log-partial', {log_id: logId}, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });


    });

    function filter() {

        var controllerId = $("#controllerId").val();
        var actionId = $("#actionId").val();
        var durationTime = $("#durationTime").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var apiType = $("#apiType").val();


        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (controllerId != '' && controllerId != null) {
            href_str = href_str + "&controllerId=" + controllerId;
        }

        if (actionId != '' && actionId != null) {
            href_str = href_str + "&actionId=" + actionId;
        }

        if (durationTime != '' && durationTime != null) {
            href_str = href_str + "&durationTime=" + durationTime;
        }

        if (startTime != '' && startTime != null) {
            href_str = href_str + "&startTime=" + startTime;
        }

        if (endTime != '' && endTime != null) {
            href_str = href_str + "&endTime=" + endTime;
        }

        if (apiType != '-1' && apiType >= 0) {
            href_str += "&apiType=" + apiType;
        }
        window.location.href = href_str;
    }

</script>

